********************************************************************************
*program for more general tables. (instead of audit variables, use indices, for example.)
********************************************************************************

*Settings
clear all
cap log close
*Change this to your system name
local user "awcassidy1"
set more off, permanently
cd "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data"



use "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/data_with_observability_indices.dta", clear

********************************************************************************
*Baseline: observability groups
********************************************************************************
*standard values
local clustvar "mlsid"
local clustvarword "Home"
local fevars "i.saleyear i.lsaleyear i.yearbuiltcat"
local fe_to_absorb="saleyear lsaleyear yearbuiltcat"
local controls "yearbuiltaudit conditionedsqft"
local baselinevars "parsimonious_less parsimonious_more_fuel parsimonious_more_other"
local filename "main"
local depvar "pricediff"
local not_extracted="duct_type  rsavg  eeravg  sizeavg    negsysageavg ah_type notwinrec  notraddr"

local parsimonious_less_vars "negleakavg atticrvalue ductravg     "
local parsimonious_more_vars_fuel "fftype whftype "
local parsimonious_more_vars_other "ah_location progtherm twosystems"


********************************************************************************
*VAR DEFINITIONS AND LABELS
********************************************************************************
gen pre_above_med_ee=above_median_ee_all_mean*pre
gen post_above_med_ee= above_median_ee_all_mean*post
la var pre_above_med_ee "Above Med EE $\times$ Sale -2"
la var post_above_med_ee "Above Med EE $\times$ Sale 0"
la var above_median_ee_all_mean "Above Med EE"
la var above_median_less "Above Med Less"

gen pre_above_med_less=above_median_less*pre
gen post_above_med_less= above_median_less*post
la var pre_above_med_less "Above Med Less $\times$ Sale -2"
la var post_above_med_less "Above Med Less $\times$ Sale 0"



	
********************************************************************************
*Above Med Less as Treatment (Table 5)
********************************************************************************

eststo clear

*Main
eststo: reghdfe pricediff c.above_median_less ///
	///
	, ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	

estadd scalar r2all = e(r2)

		
estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ P}"

	
*Placebo
eststo: reghdfe new_pricediff_baseline above_median_less, absorb(yearbuiltcat saleyear new_lsaleyear_baseline) vce(cl mlsid)	

estadd scalar r2all = e(r2)


estadd local sample="\multicolumn{1}{c}{Placebo}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ P}"
				
local b= _b[above_median_less]
local implied_pretrend: di %9.3fc `b'
estadd local implied_pretrend="`implied_pretrend'"


*3 sale
eststo: reghdfe price pre_above_med_less post_above_med_less ///
	///
	pre saleneg1 post ///
	if bal_sample==1, ///
		vce(cl mlsid) absorb(yearbuiltcat#post saleyear#post mlsid )

estadd scalar r2all = e(r2)

		
estadd local sample="\multicolumn{1}{c}{3-Sale}"
estadd local dep_var = "\multicolumn{1}{c}{P}"
				
local b= -1 * _b[pre_above_med_less]
local implied_pretrend: di %9.3fc `b'
estadd local implied_pretrend="`implied_pretrend'"

local keepvars above_median_less pre_above_med_less post_above_med_less 
		

esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/above_med_less_only.tex", replace fragment ///
		keep(`keepvars') order(`keepvars') ///
		 label booktabs nomtitles noobs se(4) b(4) ///
		scalars( ///
				"implied_pretrend Implied Pre-trend" ///
					"sample Sample" ///
					"dep_var Dep Var" ///
				"r2all R-squared" ///
				"N_clust `clustvarword' Clusters" "N Observations"  ///
				) ///
				sfmt( 0 0 %9.3fc %9.3fc %9.0fc  %9.0fc ) star(* 0.10 ** 0.05 *** 0.01)
